package com.bootdo.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bootdo.system.dao.SysDeptMapper;
import com.bootdo.system.dao.UserMapper;
import com.bootdo.system.dao.UserRoleMapper;
import com.bootdo.system.domain.SysDeptDO;
import com.bootdo.system.domain.SysUserDO;
import com.bootdo.system.domain.UserRoleDO;
import com.bootdo.system.domain.UserTokenDO;
import com.bootdo.system.service.UserService;
import com.github.pagehelper.PageHelper;

@Transactional
@Service
public class UserServiceImp implements UserService {
	@Autowired
	UserMapper userMapper;
	@Autowired
	UserRoleMapper userRoleMapper;
	@Autowired
	SysDeptMapper sysDeptMapper;
	private static final Logger logger = LoggerFactory.getLogger(UserService.class);

	@Override
	public SysUserDO get(Long id) {
		List<Long> roleIds = userRoleMapper.listRoleId(id);
		SysUserDO user = userMapper.get(id);
		if(user != null ) {
			SysDeptDO dept = sysDeptMapper.get(user.getDeptId());
			if(dept != null) {
				user.setDeptName(dept.getName());
			}
			user.setroleIds(roleIds);
		}
		return user;
	}

	@Override
	public List<SysUserDO> list(Map<String, Object> map) {
		PageHelper.startPage(Integer.parseInt(map.get("pageNum").toString()),Integer.parseInt(map.get("pageSize").toString()));
		return userMapper.list(map);
	}

	@Override
	public int count(Map<String, Object> map) {
		return userMapper.count(map);
	}

	@Transactional
	@Override
	public int save(SysUserDO user) {
		int count = userMapper.save(user);
		Long userId = user.getUserId();
		List<Long> roles = user.getroleIds();
		userRoleMapper.removeByUserId(userId);
		List<UserRoleDO> list = new ArrayList<>();
		if(roles != null && roles.size()>0) {
			for (Long roleId : roles) {
				UserRoleDO ur = new UserRoleDO();
				ur.setUserId(userId);
				ur.setRoleId(roleId);
				list.add(ur);
			}
		}
		if (list.size() > 0) {
			userRoleMapper.batchSave(list);
		}
		return userId.intValue();
	}

	@Override
	public int update(SysUserDO user) {
		int r = userMapper.update(user);
		Long userId = user.getUserId();
		List<Long> roles = user.getroleIds();
		userRoleMapper.removeByUserId(userId);
		List<UserRoleDO> list = new ArrayList<>();
		if(roles != null && roles.size()>0) {
			for (Long roleId : roles) {
				UserRoleDO ur = new UserRoleDO();
				ur.setUserId(userId);
				ur.setRoleId(roleId);
				list.add(ur);
			}
		}
		if (list.size() > 0) {
			userRoleMapper.batchSave(list);
		}
		return r;
	}

	@Override
	public int remove(Long userId) {
		userRoleMapper.removeByUserId(userId);
		return userMapper.remove(userId);
	}

	@Override
	public boolean exit(Map<String, Object> params) {
		boolean exit;
		exit = userMapper.list(params).size() > 0;
		return exit;
	}

	@Override
	public Set<String> listRoles(Long userId) {
		return userMapper.listRoles(userId);
	}

	@Override
	public int resetPwd(SysUserDO user) {
		int r = userMapper.update(user);
		return r;
	}

	@Transactional
	@Override
	public int batchremove(List<Long> userIds) {
		int count = userMapper.batchRemove(userIds);
		userRoleMapper.batchRemoveByUserId(userIds);
		return count;
	}

	@Override
	public SysUserDO login(String userName, String password) {
		// TODO Auto-generated method stub
		Map<String,String> param = new HashMap<String,String>();
		param.put("userName", userName);
		param.put("password", password);
		return userMapper.getByLoginName(param);
	}

	public int saveToken(UserTokenDO ut) {
		return userMapper.saveToken(ut);
	}

	@Override
	public Long getTokenId(Long userId) {
		// TODO Auto-generated method stub
		return userMapper.getUserTokenId(userId);
	}

	@Override
	public UserTokenDO getUserToken(Long userId) {
		// TODO Auto-generated method stub
		return userMapper.getUserToken(userId);
	}

	@Override
	public Long getUserIdByTokenId(Long tokenId) {
		return userMapper.getUserIdByTokenId(tokenId);
	}
}
